console.log('今日头条新闻插件脚本注入');

  // ============> utils

// 提取文章类新闻详情页
function extractNewDetail(){
  const rootDom = document.querySelector('.article-content');
  const article = rootDom.querySelector('article.tt-article-content.syl-article-base');
  if (article){
    const contents = {
      text: '',
      image: [],
      video: []
    }
    // const test = Array.from(article.querySelectorAll('p, h1.pgc-h-arrow-right')).map(item => item.textContent);
    // console.log(test)
    const texts = Array.from(article.querySelectorAll('p, h1.pgc-h-arrow-right')).reduce((texts, currentP) => {
      if (currentP.textContent.trim() && !currentP.className.includes('pgc-img-caption')){
        texts += currentP.textContent.trim() + '\n';
      }
      return texts;
    }, '');
    contents.text = texts;
    console.log('新闻文本内容如下： ');
    console.log(contents.text);
    const imgs = Array.from(article.querySelectorAll('img.syl-page-img'));
    if (imgs.length > 0){
      // let loadingClassReg = /syl-img-loading/ig;
      imgs.forEach(img => {
        // console.log(!!(loadingClassReg.test(img.className) && img.dataset.src));
        // console.log(img.dataset.src)
        // console.log(img.src)
        // if (loadingClassReg.test(img.className)){
        //   console.log('保存img.dataset.src')
        //   contents.image.push(img.dataset.src)
        // } else if (img.src){
        //   console.log('保存img.src')
        //   contents.image.push(img.src);
        // }
        
        if (img.dataset.src){
          contents.image.push(img.dataset.src);
        } else {
          contents.image.push(img.src);
        }
      })
    }
    console.log('新闻图片内容如下： ');
    console.log(contents.image);
    const videos = Array.from(article.querySelectorAll('.tt-video-box > video'));
    if (videos.length > 0){
      videos.forEach(video => {
        // console.log('收集到的视频项是否存在src： ')
        // console.log(!!video.src)
        if (video.src){
          contents.video.push(video.src);
        }
      });
    }
    console.log('新闻视频内容如下： ');
    console.log(contents.video)
    return contents;
  } else {
    return false;
  }
}

// 提取视频类新闻详情页
function extractVideoDetail(){
  const rootDom = document.querySelector('.main-content');
  const videos = Array.from(rootDom.querySelectorAll('div.ttp-xgplayer > video'));
  const contents = {
    text: '',
    image: [],
    video: []
  }
  if (videos.length > 0){
    videos.forEach(video => {
      if (video.src){
        contents.video.push(video.src);
      }
    });
    console.log('新闻的视频信息如下： ');
    console.log(contents.video)
    return contents;
  } else {
    return false;
  }
}

// 提取微头条类新闻详情页
function extractWeiDetail(){
  const rootDom = document.querySelector('.wtt-detail-container');
  const articleContainer = rootDom.querySelector('.wtt-content > article');
  if (articleContainer){
    let contents = {
      text: '',
      image: [],
      video: []
    };
    const textOuter = articleContainer.querySelector('.weitoutiao-html');
    const textOuterChildNodes = Array.from(textOuter.childNodes);
    contents.text = textOuterChildNodes.reduce((texts, currentNode) => {
      if (currentNode.nodeType === 3 && currentNode.textContent){
        texts += currentNode.textContent.trim() + '\n';
      }
      return texts;
    }, '');
    console.log('新闻的文本信息如下： ');
    console.log(contents.text);
    const imgs = Array.from(articleContainer.querySelectorAll('.image-list > .weitoutiao-img'));
    if (imgs.length > 0){
      imgs.forEach(img => {
        if (img.dataset.src){
          contents.image.push(img.dataset.src);
        } else {
          contents.image.push(img.src);
        }
      });
    }
    console.log('新闻的图片信息如下： ');
    console.log(contents.image)
    return contents;
  } else {
    return false;
  }
}

//处理新闻请求
function handleNew(){
  // 文章 -  /article/
  // 视频 - /video/
  // 微头条 - /w/
  let articleReg = /^\/article\//i;
  let videoReg = /^\/video\//i;
  let weiArticleReg = /^\/w\//i;
  if (articleReg.test(window.location.pathname)){
    return extractNewDetail();
  } else if (videoReg.test(window.location.pathname)){
    return extractVideoDetail();
  } else if (weiArticleReg.test(window.location.pathname)) {
    return extractWeiDetail();
  } else {
    return false;
  }
}

// 消息监听
chrome.runtime.onMessage.addListener((message, sender, sendMessage) => {
  // 后台发送的采集新闻页信息消息
  if (message.id === 'newDetail'){
    const result = handleNew();
    sendMessage(result);
  }
});